'''
@Author: your name
@Date: 2020-02-10 15:19:50
@LastEditTime : 2020-02-10 16:51:30
@LastEditors  : Please set LastEditors
@Description: In User Settings Edit
@FilePath: \controlbox\common\libs\ComDeal.py
'''

WATER_CMD = {
    'x轴': ['a', '', '', '', '', ''],
    'x轴': ['a', '', '', '', '', ''],
    'x轴': ['a', '', '', '', '', ''],
}

class ComDeal():

    @staticmethod
    def getCmd(cmd_list):
        '''生成命令'''
        cmd = ''.join(cmd_list)
        cmd.upper()
        cmd_start = cmd_list[0]
        cmd_len = hex(int(len(cmd) / 2)).replace('0x', '').zfill(4).upper()
        cmd_list[1] = cmd_len
        if len(cmd_len) == 4:
            hex_len = int(cmd_len[:2], 16) + int(cmd_len[-2:], 16)
            cmd_sum = (hex(int(cmd_start, 16) + int(cmd_len[:2], 16) + int(cmd_len[-2:], 16))).upper()[-2:]
        if len(cmd_len) == 2:
            hex_len = int(cmd_len, 16)
            cmd_sum = (hex(int(cmd_start, 16) + int(cmd_len, 16))).upper()[-2:]
        cmd_list[2] = cmd_sum
        s = 0
        s = int(cmd_list[0], 16) + hex_len + int(cmd_list[2], 16) + int(cmd_list[3], 16)
        for i in range(0, len(cmd_list[4])):
            if i % 2 == 0:
                s = s + int(cmd_list[4][i:i + 2], 16)
        s = hex(s).upper()[-2:]
        cmd_list[-1] = s
        cmd = ''.join(cmd_list)
        return cmd

    @staticmethod
    def getSum(hex_str=""):
        """计算校验和,取校验和低八位"""
        if hex_str:
            s = 0
            for i in range(0, len(hex_str)):
                if i % 2 == 0:
                    s = s + int(hex_str[i:i + 2], 16)
            s = hex(s).replace('0x','').upper()
            if len(s) > 2:
                s = s[-2:]
            else:
                s = s.zfill(2)
            return s
        return False


# com = ComDeal()
# res = com.getSum(hex_str="0102")
# print(res)
